package com.ncloud.system.business;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.system.entity.Depart;
import com.ncloud.system.entity.Menu;
import com.ncloud.system.entity.User;
import com.ncloud.system.service.MenuService;
import com.ncloud.system.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by shiqiang on 2017/8/1.
 */
@Component
public class MenuBusi {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    public static final Long TOP_LEVEL=0L;
    @Autowired
    private MenuService menuService;
    @Autowired
    private UserService userService;
    /*获取可选择的菜单列表*/
    public List<Map<String,?>> getSelectMenus(){
        List<Map<String,?>> list = new ArrayList<>();
        List<Menu> topMenus = menuService.getMenusByLevel(TOP_LEVEL);
        topMenus.stream().filter(menu -> menu.getMenuId() != 0L).forEach(menu -> {
            logger.info(JSON.toJSONString(menu));
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("topMenu",menu);
            List<Menu> secMenus = menuService.getMenusByLevel(menu.getMenuId());
            map.put("secMenu",secMenus);
            list.add(map);
        });
        return list;
    }

    /*获取登录用户已经选择的菜单列表*/
    public List<Map<String,Object>> getSelectMenusByRole(Long roleId){
        List<Map<String,Object>> list = new ArrayList<>();
        /*已结选择菜单*/
        List<Menu> userMenus = menuService.getMenusByRole(roleId);
        Set<Long> set = userMenus.stream().filter(menu -> menu.getMenuId() != 0L).map(menu -> menu.getMenuId()).collect(Collectors.toSet());
        List<Menu> topMenus = menuService.getMenusByLevel(TOP_LEVEL);
        topMenus.stream().filter(menu -> menu.getMenuId() != 0L).forEach(menu -> {
            Map<String,Object> map = new HashMap<String, Object>();
            /*用户是否已经选择*/
            if(set.contains(menu.getMenuId())){
                menu.setSelectState(true);
            }else{
                menu.setSelectState(false);
            }
            map.put("topMenu",menu);
            List<Menu> secMenus = menuService.getMenusByLevel(menu.getMenuId());
            secMenus.stream().forEach(menu1 -> {
                if(set.contains(menu1.getMenuId())){
                    menu1.setSelectState(true);
                }else {
                    menu1.setSelectState(false);
                }
            });
            map.put("secMenu",secMenus);
            list.add(map);
        });
        return list;
    }

    public JSONObject getUserMenu(Long userId){
        JSONObject json = new JSONObject();
        //这里根据用户获取用户信息，这里暂时写死
        User user = userService.findByUserId(userId);
       /*部门:如果是系统超级管理员，查询所有，否则查询自己组织*/
        List<Menu> menus = null;
        if("1".equals(user.getSuperman())){
            json.put("admin",true);
            menus = menuService.getMenusAll();
        }else{
            json.put("admin",false);
            menus = menuService.getMenusByUser(userId);
        }
        json.put("menu_info",menus);
        return json;
    }

    public List<Menu> getViewMenu(Long userId){
        List<Menu> menus =  menuService.getMenusByUser(userId);
        return menus;
    }
}
